package com.filetransfer.api.download;
import lombok.extern.slf4j.Slf4j;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
@Slf4j
public class fileDownload {
    public static void main(String[] args) {
        String fileUrl = "https://example.com/file.zip";
        String savePath = "file.zip";
        try {
            downloadFile(fileUrl, savePath);
            log.info("文件下载完成！");
        } catch (IOException e) {
            log.error("文件下载失败：" + e.getMessage());
        }
    }
    public static void downloadFile(String fileUrl, String savePath) throws IOException {
        URL url = new URL(fileUrl);
        URLConnection connection = url.openConnection();
        try (InputStream inputStream = connection.getInputStream();
             FileOutputStream fileOutputStream = new FileOutputStream(savePath)) {
            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                fileOutputStream.write(buffer, 0, bytesRead);
            }
        }
    }
}
